package TCP.Server;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        System.out.println("UDP.Server started");
        Socket socket = serverSocket.accept();
        System.out.println("UDP.Client connected" + socket.getRemoteSocketAddress());
        OutputStream outputStream = socket.getOutputStream();
        InputStream inputStream = socket.getInputStream();
        var writer = new BufferedWriter(new OutputStreamWriter(outputStream));
        var reader = new BufferedReader(new InputStreamReader(inputStream));
        writer.write("hello" + System.lineSeparator());
        writer.flush();
        try {
            for (; ; ) {
                String line = reader.readLine();
                if (line.equals("exit")) {
                    break;
                }
                System.out.println("get message from " + socket.getRemoteSocketAddress() + ": " + line);
                writer.write("ok: " + line);
                writer.newLine();
                writer.flush();
            }
        } catch (Exception e) {

        }
        System.out.println("UDP.Server stopped");
        serverSocket.close();
    }
}
